package com.example.config;

import com.example.annotation.ShiroData;
import com.example.annotation.TestData;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.data.transaction.ChainedTransactionManager;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import javax.sql.DataSource;

@EnableTransactionManagement
@Configuration
public class DataSourceConfig implements TransactionManagementConfigurer {
    @Primary
    @Bean
    public DataSource shiroDataSource() {
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setUsername("root");
        hikariDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=UTF-8");
        hikariDataSource.setPassword("123456");
        hikariDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        hikariDataSource.setIdleTimeout(60L);
        hikariDataSource.setMaxLifetime(1800000L);
        hikariDataSource.setMaximumPoolSize(5);
        hikariDataSource.setConnectionTestQuery("SELECT 1");
        return hikariDataSource;
    }

    @Bean
    public DataSource testDataSource() {
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setUsername("root");
        hikariDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8");
        hikariDataSource.setPassword("123456");
        hikariDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        hikariDataSource.setIdleTimeout(60L);
        hikariDataSource.setMaxLifetime(1800000L);
        hikariDataSource.setMaximumPoolSize(5);
        hikariDataSource.setConnectionTestQuery("SELECT 1");
        return hikariDataSource;
    }

    @Bean
    public DataSourceTransactionManager testTransactionManager() {
        return new DataSourceTransactionManager(this.testDataSource());
    }

    @Bean
    public DataSourceTransactionManager shiroTransactionManager() {
        return new DataSourceTransactionManager(this.shiroDataSource());
    }
    @Primary
    @Bean
    public SqlSessionFactory sqlSessionFactoryBeanShiro() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(this.shiroDataSource());
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setCallSettersOnNulls(true);
        configuration.setDefaultStatementTimeout(Integer.valueOf(600));
        sqlSessionFactoryBean.setConfiguration(configuration);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:com/example/*/mapper/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBeanTest() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(this.testDataSource());
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setCallSettersOnNulls(true);
        configuration.setDefaultStatementTimeout(Integer.valueOf(600));
        sqlSessionFactoryBean.setConfiguration(configuration);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:com/example/*/mapper/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurerTest() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBeanTest");
        mapperScannerConfigurer.setBasePackage("com.example.mapper");
        mapperScannerConfigurer.setAnnotationClass(TestData.class);
        return mapperScannerConfigurer;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurerShiro() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBeanShiro");
        mapperScannerConfigurer.setBasePackage("com.example.mapper");
        mapperScannerConfigurer.setAnnotationClass(ShiroData.class);
        return mapperScannerConfigurer;
    }

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        ChainedTransactionManager ctm = new ChainedTransactionManager(new PlatformTransactionManager[]{this.testTransactionManager(),this.shiroTransactionManager() });
        return ctm;
    }



}
